package com.breakfast.fun.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.breakfast.fun.App;
import com.breakfast.fun.R;
import com.breakfast.fun.data.entity.home.CenterAd;
import com.breakfast.fun.ui.web.WebActivity;
import com.breakfast.fun.util.L;
import com.bumptech.glide.Glide;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页Banner适配器
 * Created by hp on 2016/6/8.
 */
public class HomeBannerAdapter extends PagerAdapter {


    private Context context;
    private List<CenterAd> bannerList;
    private List<ImageView> imageViewList = new ArrayList<>();
    //跟踪器
    private Tracker mTracker;

    public HomeBannerAdapter(Context context, List<CenterAd> bannerList) {
        this.context = context;
        this.bannerList = bannerList;
        for (int i = 0; i < bannerList.size(); i++) {
            imageViewList.add(new ImageView(context));
        }
        mTracker = App.getApp().getDefaultTracker();
    }

    @Override
    public int getCount() {
        //为了正向无限循环滚动，将数量设置为无限大
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    /**
     * 创建view
     *
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //对viewpager页号求模取出view列表中要显示的项
        if (bannerList.size() > 0) {
            position %= bannerList.size();

            if (position < 0) {
                position = bannerList.size() + position;
            }

            ImageView imageView = imageViewList.get(position);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            //如果view已经在之前添加到了一个父组件，则
            //必须先remove,否则会抛出IllegalStateException
            ViewPager vp = (ViewPager) imageView.getParent();
            if (vp != null) {
                ViewGroup parent = vp;
                parent.removeView(imageView);
            }

            container.addView(imageView);

            CenterAd ad = bannerList.get(position);

            /*显示图片*/
            Glide.with(App.getAppContext())
                    .load(ad.getPicture())
                    .placeholder(R.drawable.bg_cache_banner)
                    .error(R.drawable.bg_cache_banner)
                    .crossFade()
                    .into(imageView);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    L.d("click");
                    //发送点击事件给谷歌统计
                    mTracker.send(new HitBuilders.EventBuilder()
                            .setCategory("Action")
                            .setAction("Android-点击Banner页-" + ad.getTitle())
                            .build());
                }
            });

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //如果，banner类型为ad
                    //则代表这是一个可跳转的网页链接
                    if (ad.getTypeLable().equals("ad")) {
                        Intent intent = new Intent(context, WebActivity.class);
                        intent.putExtra("title", ad.getTitle());
                        intent.putExtra("url", ad.getLinkUrl());
                        intent.putExtra("des", ad.getInfo());
                        //如果链接的URL中含有recharge字段，就说明是充值活动页面
                        //因为后台没有类型说明，移动端只能这么判断了
                        if (ad.getLinkUrl().contains("recharge")) {
                            intent.putExtra("recharge", true);
                        } else {
                            intent.putExtra("recharge", false);
                        }
                        context.startActivity(intent);
                    }
                }
            });

            return imageView;
        }
        return null;
    }

    /**
     * 销毁view
     *
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
       /* ImageView imageView = viewList.get(position);
        container.removeView(imageView);*/
    }

    public void replaceAll(List<CenterAd> bannerList) {
        this.bannerList.clear();
        this.imageViewList.clear();
        this.bannerList = bannerList;
        for (int i = 0; i < bannerList.size(); i++) {
            imageViewList.add(new ImageView(context));
        }
    }
}
